﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.CustomerService.BLL;
using Koonsoft.CustomerService.Entity;
using System.Text;

namespace CustomerService.ReportIssues
{
    public partial class IssueEdit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid guid = Guid.Empty;
                try
                {
                    guid = Guid.Parse(Request["Guid"].ToString());

                    string msg;
                    Issue issue = new IssuesBLL().GetIssues(new Issue() { IssueID = guid, Status = -1 }, out msg).FirstOrDefault();


                    labClientVersionValue.Text = issue.ClientVersion;
                    labCreateUser.Text = issue.CreateUser;
                    labCustomersMobile.Text = issue.CustomersMobile;
                    labCustomersQQ.Text = issue.CustomersQQ;
                    labDecodingRate.Text = issue.DecodingRate;
                    labExceptionTime.Text = issue.ExceptionTime.ToString("yyyy-MM-dd HH:mm");
                    labIssueCategory.Text = issue.IssueCategory;
                    labIssueLevel.Text = issue.IssueLevel;
                    labIssueSource.Text = issue.IssueSource;
                    labIssueTitle.Text = issue.IssueTitle;
                    labIsUnblocked.Text = issue.IsUnblocked == true ? "畅通" : "不畅通";
                    labRegion.Text = issue.Region;
                    labServiceArea.Text = issue.ServiceArea;
                    labStatus.Text = new WFBLL().GetWfById(issue.Status).State;
                    labSystemEnvironment.Text = issue.SystemEnvironment;

                    labBandWidth.Text = string.Format("{0} {1}", issue.OperatorsId, issue.BandWidth);
                    labUserName.Text = issue.UserName;
                    labUserAccounts.Text = issue.UserAccounts;
                    labGSID.Text = issue.GSID;
                    labId.Text = issue.ID;
                    labPrincipal.Text = issue.PrincipalID;




                    txtMark.Text = issue.Mark.Replace("\n", "<br />");

                    List<IssueAccessorie> issueAccessories = new IssueAccessoriesBLL().GetIssueAccessorie(guid, out msg);

                    if (issueAccessories != null && issueAccessories.Count > 0)
                    {
                        StringBuilder sbAccessories = new StringBuilder();
                        sbAccessories.Append("附件:");
                        foreach (var acc in issueAccessories)
                        {
                            sbAccessories.AppendFormat("<div><a href='{1}'>{0}</a></div>", acc.AccessoryName, acc.Accessory);
                        }
                        labAccessories.Text = sbAccessories.ToString().Replace("~/", "../");
                    }


                    List<IssueStateUpdateLog> logs = new IssueStateUpdateLogBLL().GetLog(guid);

                    StringBuilder sb = new StringBuilder();
                    sb.Append("<table>");
                    foreach (var log in logs)
                    {
                        sb.AppendFormat(@"<tr>
                                             <td>{0} -> {1} </td>
                                             <td>处理人：</td><td>{2}</td>
                                             <td>处理时间：</td><td>{3}</td>
                                         </tr>"
                                    , log.OldState, log.NewState, log.UserName, log.Time.ToString("yyyy-MM-dd"));

                        sb.AppendFormat("<tr><td>备注：</td><td>{0}</td></tr>", log.Mark.Replace("\n", "<br />"));
                    }
                    sb.Append("</table>");

                    Label1.Text = sb.ToString();

                    Button1.Text = new WFBLL().GetWfById(issue.Status).NextLink;
                    if (Button1.Text == "")
                    {
                        Button1.Visible = false;
                    }
                }
                catch
                {
                    AlterCloseReturn("GUID错误", "");
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Redirect(labStateUpdateUrl.Text + "?GUID=" + Request["Guid"].ToString());
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Redirect(labIssuesEditUrl.Text + "?GUID=" + Request["Guid"].ToString());
        }
    }
}